package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.WashPointType;
import com.cloudfun.campusshare.common.model.vo.WashPointAddVO;
import com.cloudfun.campusshare.common.model.vo.WashPointExportVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.entity.UserEntity;
import com.cloudfun.campusshare.repo.UserEntityRepo;
import com.cloudfun.campusshare.service.WashPointService;
import com.cloudfun.campusshare.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/4/28 17:56
 * @description : 洗衣点
 */
@RestController
@RequestMapping("/v1/washPoint")
@Api(value = "洗衣点")
@Validated
@Slf4j
@AuthRequired
public class WashPointController {

    @Autowired
    private WashPointService washPointService;

    @Resource
    private UserEntityRepo userEntityRepo;

    @ApiOperation(value = "添加洗衣点")
    @PostMapping("/add")
    @ResponseBody
    public BaseResult addWashPoint(@RequestBody @Valid WashPointAddVO washPointAddVO){
        washPointService.addWashPoint(washPointAddVO);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "洗衣点列表", notes = "洗衣点列表")
    @GetMapping("/list")
    public BaseResult queryWashPointList(Pageable pageable,@ApiParam(value = "关键字") @RequestParam(required = false) String keyword,
                                         @ApiParam(value = "洗衣点类型集合")@RequestParam(required = false)Set<WashPointType> type,
                                         @ApiParam(value = "运营商id")@RequestParam(required = false)String operatorId){
        PageResult pageResult = washPointService.queryWashPointList(pageable, keyword,type,operatorId);
        return BaseResultFactory.produceSuccess(pageResult);
    }

    @ApiOperation(value = "获取洗衣点列表（查询条件）", notes = "获取洗衣点列表（查询条件）")
    @GetMapping("/query/list")
    public BaseResult queryWashPointListForDevice(@ApiParam(value = "关键字") @RequestParam(required = false) String keyword,@ApiParam(value = "运营商id")@RequestParam(required = false)String operatorId){
        return BaseResultFactory.produceSuccess( washPointService.getWashPointList(keyword,operatorId));
    }

    @ApiOperation(value = "删除洗衣点", notes = "删除洗衣点")
    @PostMapping("/delete/{washPointId}")
    public BaseResult deleteWashPoint(@PathVariable String washPointId){
        washPointService.deleteWashPoint(washPointId);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "获取洗衣点详情", notes = "获取洗衣点详情")
    @GetMapping("/detail/{washPointId}")
    public BaseResult queryWashPointDetail(@PathVariable String washPointId){
        return BaseResultFactory.produceSuccess(washPointService.queryWashPointDetail(washPointId));
    }

    @ApiOperation(value = "洗衣点批量导出", notes = "洗衣点批量导出")
    @PostMapping("/batch/export")
    public BaseResult batchExport(HttpServletResponse response,@ApiParam(value = "洗衣点id集合") @RequestParam(required = false) Set<String> washPointIds,
                                  @ApiParam(value = "关键字") @RequestParam(required = false) String keyword,
                                  @ApiParam(value = "所有") @RequestParam(value = "是否导出所有", required = false) Boolean all,
                                  @ApiParam(value = "用户id") @RequestParam(required = false) String userId) throws IOException {

        if (CollectionUtils.isEmpty(washPointIds) && StringUtils.isNotBlank(keyword) && BooleanUtils.isTrue(all)) {
            return BaseResultFactory.produceEmptyResult(Code.PARAMETER_ERROR, "参数错误");
        }
        List<WashPointExportVO> washPointExportVOS = washPointService.exportWashPoint(washPointIds, keyword, all, userId);
        Optional<UserEntity> userEntity = userEntityRepo.findById(AuthenticationHolder.getUserId());
        String prefix = userEntity.get().getEnterpriseName();
        ExcelUtils.exportExcel(washPointExportVOS,null,"洗衣点信息",WashPointExportVO.class,prefix+"-洗衣点信息",response);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "用户预约获得洗衣点列表", notes = "用户预约获得洗衣点列表")
    @GetMapping("/user/pickList")
    public BaseResult userPickWashPointList(@ApiParam(value = "洗衣点类型") @RequestParam WashPointType type,
                                            @ApiParam(value = "用户当前位置经度")@RequestParam String longitude,
                                            @ApiParam(value = "用户当前位置纬度")@RequestParam String latitude){
        return BaseResultFactory.produceSuccess(washPointService.getUserPickList(type, longitude, latitude));
    }

}
